package org.jcb;

import java.util.List;

import org.jcb.dao.data.ChineseDatabase;
import org.jcb.dao.data.GlobalDAO;
import org.jcb.dao.data.StrategyData;
import org.jcb.model.ChineseQuestionnaire;
import org.jcb.model.DataChineseLanguage;
import org.jcb.model.User;
import org.jcb.service.GenerateQuestionnaireRandomStrategy;
import org.jcb.view.AdminView;
import org.jcb.view.LearningView;
import org.jcb.view.ButtonChoixQuizzView;
import org.jcb.view.QuizzView;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class ChinoisActivity extends Activity {
	ThreadController workerThread ;
	ChiniseView view ;
	LearningView learningView ;
	//Question question ;
    private Button mLearnButton, mAutoControButton, mQuizzButton, mquizzChineseCharacters, mAutocontrolePinyin,mlearnLevel1, buttonQuizzPinyin ;
    private ListView mContactList;
    private boolean mShowInvisible;
    List<DataChineseLanguage> data = ChineseDatabase.loadData();
    //ChineseQuestionnaire cq ;
    ArrayAdapter<String> adapter ;
    public static MediaPlayer mediaPlayer ;
	private static Context context;
    View viewInitialMenu ;
	View optionsLEarnView ;
	private User user = new User("jcb");

	public static GlobalDAO globalDAO ;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("", "on create=" + Thread.currentThread().getName());
        initInitialMenus();
        workerThread = new ThreadController();
        workerThread.start();
        Log.i("WXW", "before globalDAO=" + Thread.currentThread().getName());
        globalDAO = new GlobalDAO(this);
        Log.i("WXW", "after globalDAO=" + Thread.currentThread().getName());
        context=getApplicationContext();
        mediaPlayer = MediaPlayer.create(context, R.raw.bip);
    }
    
    public void onResume() {
    	super.onResume();
    	Log.i("", "on resume=" + Thread.currentThread().getName());
    }
    
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
    
    
    public void initInitialMenus() {
        setContentView(R.layout.initial_menu);
        Log.i("", "view=" + Thread.currentThread().getName());
        mLearnButton = (Button) findViewById(R.id.learnButton);
        mAutoControButton = (Button) findViewById(R.id.autoControlButton);
        mQuizzButton  = (Button) findViewById(R.id.quizzButton);
        mAutocontrolePinyin = (Button) findViewById(R.id.autocontrolePinyin);
        buttonQuizzPinyin = (Button) findViewById(R.id.buttonQuizzPinyin);
        mquizzChineseCharacters = (Button) findViewById(R.id.quizzChineseCharacters);
        mlearnLevel1 = (Button) findViewById(R.id.learnLevel1);

        
        findViewById(R.id.linearLayoutQuizz).setVisibility(View.GONE);
        findViewById(R.id.linearLayoutLearn).setVisibility(View.GONE);
        findViewById(R.id.linearLayoutAutocontrole).setVisibility(View.GONE);

        mAutocontrolePinyin.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.d("TAG", "mAddAccountButton clicked");
                launchAutoControl();
            }
        });

        
        buttonQuizzPinyin.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.d("TAG", "mAddAccountButton clicked");
                launchMenuQuizzPinyin();
            }
        });        
        
        
        mlearnLevel1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.d("TAG", "mAddAccountButton clicked");
                launchLearn(1);
            }
        });        
        
        mAutoControButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.d("TAG", "mAddAccountButton clicked");
                findViewById(R.id.linearLayoutAutocontrole).setVisibility(View.VISIBLE);
                findViewById(R.id.linearLayoutLearn).setVisibility(View.GONE);
                findViewById(R.id.linearLayoutQuizz).setVisibility(View.GONE);

                
                
                //launchAutoControl();
            }
        });

        mQuizzButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.d("TAG", "mAddAccountButton clicked");
                findViewById(R.id.linearLayoutQuizz).setVisibility(View.VISIBLE);
                findViewById(R.id.linearLayoutLearn).setVisibility(View.GONE);
                findViewById(R.id.linearLayoutAutocontrole).setVisibility(View.GONE);
                
                //findViewById(R.id.linearLayoutLearn).setVisibility(View.GONE);
            }
        });        
        
        mLearnButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.d("TAG", "mAddAccountButton clicked ");
                findViewById(R.id.linearLayoutLearn).setVisibility(View.VISIBLE);
                findViewById(R.id.linearLayoutQuizz).setVisibility(View.GONE);
                findViewById(R.id.linearLayoutAutocontrole).setVisibility(View.GONE);
            }
        }); 
    	
    }
    
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.homeMenu:
            	initInitialMenus();
            	//setContentView(R.layout.initial_menu);
                return true;
            case R.id.aboutMenu:
            	setContentView(R.layout.about);     
                return true;
            case R.id.settingMenu:    
            	initInitialMenus();
            case R.id.adminMenu:    
            	launchAdmin();                
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	Log.i("", "onCreateOptionsMenu" + Thread.currentThread().getName());
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menus, menu);
        return true;
    }
    
    public void launchAutoControl() {
    	StrategyData strategyData = new StrategyData();
    	strategyData.setRandom(true) ;
    	ChineseQuestionnaire cq = GenerateQuestionnaireRandomStrategy.generate(data,  strategyData);
        view = new ChiniseView(this) ;
        view.setQuestionnaire(cq);
        setContentView(view);
    }

    public void launchAdmin() {
    	AdminView adminView  = new AdminView(this);
    	setContentView(adminView);
    	
    }        
    
    private final static int ID_TYPE_QUIZZ_PINYIN = 1 ;
    
    public void launchMenuQuizzPinyin() {
    	ButtonChoixQuizzView menuQuizzPinyinView  = new ButtonChoixQuizzView(this, this, user,ID_TYPE_QUIZZ_PINYIN);
    	setContentView(menuQuizzPinyinView);
    	
    }    
    
    public void launchQuizzPinyin(int idTypeQuizz, int numQuizz) {
    	Integer idQuizz = globalDAO.getIdQuizz(idTypeQuizz, numQuizz);
    	//ChineseQuestionnaire cq = new 
    	ChineseQuestionnaire cq = globalDAO.getQuestionnaire(idQuizz) ;
    	QuizzView quizzPinyinView = new QuizzView(this,cq);
    	setContentView(quizzPinyinView);
    }
    
    
    public void endQuizzPinyin(ChineseQuestionnaire questionnaire) {
    	Log.i("ChinoisActivity", "endQuizzPinyin debut");
    	globalDAO.recordQuizzResult(questionnaire, user);
    	Log.i("ChinoisActivity", "before launchMenuQuizzPinyin");
    	launchMenuQuizzPinyin();    	      
    }    
    
    public void launchLearn(int level) {
    	StrategyData strategyData = new StrategyData();
    	strategyData.setRandom(false) ;
    	strategyData.setNbDataExtact(20);
    	strategyData.setLevel1(true);
    	strategyData.setNeverEnding(true);
    	List<DataChineseLanguage> data = ChineseDatabase.loadData();
    	ChineseQuestionnaire cq = GenerateQuestionnaireRandomStrategy.generate(data,  strategyData);
    	LearningView learningView = new LearningView(this, cq);
    	setContentView(learningView);
    	
    }    

    
    public void showResult() {
    	setContentView(R.layout.result);     
    }
    
    
    
    @Override
    public void onDestroy() {
        Log.i("", "on destroy=" + Thread.currentThread().getName());
        try {
	        if (workerThread != null) {
	        	workerThread.stopThread();
	        }
        }
        catch (Exception e) {
        	Log.i("", e.toString());
        }
        mediaPlayer.release();
        mediaPlayer = null;
        super.onDestroy();
    }
    
    
    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        //view.setScreenConfigAdjusted(false);
    }    
    
    
}